import AppLayout from '@/layout/AppLayout.vue';
import { createRouter, createWebHistory } from 'vue-router';
import { useUserStore } from "@/store/user"

const title = import.meta.env.VITE_APP_TITLE
const defaultRoutePath = '/admin'
const whiteRoute = ['login', 'mineDoc', 'interfaceList', 'interfaceCode', 'signature', 'landing']

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: '/',
            name: 'landing',
            component: () => import('@/views/pages/Landing.vue')
        },
        {
            path: '/admin',
            component: AppLayout,
            children: [
                {
                    path: '/config',
                    component: AppLayout,
                    children: [
                        {
                            path: '/config/config',
                            name: 'config_config',
                            title: '网站设置',
                            component: () => import('@/views/config/Config.vue')
                        },
                        {
                            path: '/config/api',
                            name: 'config_api',
                            title: '接口设置',
                            component: () => import('@/views/config/Api.vue')
                        },
                    ]
                },
                {
                    path: '/dashboard',
                    name: 'dashboard',
                    component: () => import('@/views/Dashboard.vue')
                },
                {
                    path: '/uikit/formlayout',
                    name: 'formlayout',
                    component: () => import('@/views/uikit/FormLayout.vue')
                },
                {
                    path: '/uikit/input',
                    name: 'input',
                    component: () => import('@/views/uikit/InputDoc.vue')
                },
                {
                    path: '/uikit/button',
                    name: 'button',
                    component: () => import('@/views/uikit/ButtonDoc.vue')
                },
                {
                    path: '/uikit/table',
                    name: 'table',
                    component: () => import('@/views/uikit/TableDoc.vue')
                },
                {
                    path: '/uikit/list',
                    name: 'list',
                    component: () => import('@/views/uikit/ListDoc.vue')
                },
                {
                    path: '/uikit/tree',
                    name: 'tree',
                    component: () => import('@/views/uikit/TreeDoc.vue')
                },
                {
                    path: '/uikit/panel',
                    name: 'panel',
                    component: () => import('@/views/uikit/PanelsDoc.vue')
                },

                {
                    path: '/uikit/overlay',
                    name: 'overlay',
                    component: () => import('@/views/uikit/OverlayDoc.vue')
                },
                {
                    path: '/uikit/media',
                    name: 'media',
                    component: () => import('@/views/uikit/MediaDoc.vue')
                },
                {
                    path: '/uikit/message',
                    name: 'message',
                    component: () => import('@/views/uikit/MessagesDoc.vue')
                },
                {
                    path: '/uikit/file',
                    name: 'file',
                    component: () => import('@/views/uikit/FileDoc.vue')
                },
                {
                    path: '/uikit/menu',
                    name: 'menu',
                    component: () => import('@/views/uikit/MenuDoc.vue')
                },
                {
                    path: '/uikit/charts',
                    name: 'charts',
                    component: () => import('@/views/uikit/ChartDoc.vue')
                },
                {
                    path: '/uikit/misc',
                    name: 'misc',
                    component: () => import('@/views/uikit/MiscDoc.vue')
                },
                {
                    path: '/uikit/timeline',
                    name: 'timeline',
                    component: () => import('@/views/uikit/TimelineDoc.vue')
                },
                {
                    path: '/pages/empty',
                    name: 'empty',
                    component: () => import('@/views/pages/Empty.vue')
                },
                {
                    path: '/pages/crud',
                    name: 'crud',
                    component: () => import('@/views/pages/Crud.vue')
                },
                {
                    path: '/documentation',
                    name: 'documentation',
                    component: () => import('@/views/pages/Documentation.vue')
                }
            ]
        },
        {
            path: '/pages/notfound',
            name: 'notfound',
            component: () => import('@/views/pages/NotFound.vue')
        },

        {
            path: '/auth/login',
            name: 'login',
            component: () => import('@/views/pages/auth/Login.vue')
        },
        {
            path: '/auth/access',
            name: 'accessDenied',
            component: () => import('@/views/pages/auth/Access.vue')
        },
        {
            path: '/auth/error',
            name: 'error',
            component: () => import('@/views/pages/auth/Error.vue')
        }
    ]
});

router.beforeEach(async (to, from, next) => {
    console.log(to);
    const userStore = useUserStore()
    let toTitle = to.meta.title ? to.meta.title : to.name
    document.title = `${toTitle} - ${title}`

    const token = userStore.token
    
    // 登录状态下
    if (token) {
      if (to.name === 'login') {
        next({ path: defaultRoutePath })
        return
      }
  
      if (! userStore.userInfo && userStore.userInfo == undefined ) {
        const data = await userStore.requestUserInfo()
        data && next({ path: to.path, query: to.query })
      } else {
        next()
      }
    } else {
      // 未登录的情况下允许访问的路由
      console.log(whiteRoute)
      console.log(to.name)
      console.log(whiteRoute.includes(to.name));
      if (! whiteRoute.includes(to.name)) {
        next({ name: 'login', query: { redirect: to.fullPath } })
      } else {
        next()
      }
    }
  })
  
  router.afterEach((to, from) => {
    
  })
  
  router.onError(error => {
    console.log(error)
  });
export default router;
